{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# NumPy进阶修炼80题\n",
    "## 作者：刘早起\n",
    "## 公众号：早起Python\n",
    "\n",
    "未经授权严禁转载/引流等任何侵权行为，侵权必纠！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第一期｜热身20题:数据创建与访问"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.导入并查看NumPy版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.15.4\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "print(np.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.创建十个全为0的一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.创建10个全为0的一维数据并修改数据类型为整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(10,dtype = 'int')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.创建20个0-100固定步长的数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,\n",
       "       85, 90, 95])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0,100,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.从list创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "List = [1,2,3,4,5,6,7,8,9]\n",
    "result = np.array(List)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.创建一个三行三列全是1的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#方法1\n",
    "np.ones((3,3))\n",
    "#方法2\n",
    "np.array([[ 1., 1., 1.],\n",
    "       [ 1., 1., 1.],\n",
    "       [ 1., 1., 1.]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.创建一个2行2列矩阵并且元素为布尔类型的True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True],\n",
       "       [ True,  True]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((2,2), True, dtype=bool)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.创建等差数列\n",
    "备注：从5开始，50结束，共10个数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5., 10., 15., 20., 25., 30., 35., 40., 45., 50.])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(start=5,stop=50,num=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 9.创建等差数列\n",
    "备注：从5开始，50结束，共10个数据，数据类型为int32\n",
    "思考：与上一题不同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50], dtype=int32)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(start = 5, stop = 55, step = 5,dtype = 'int32')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.创建3x3矩阵\n",
    "备注：矩阵元素均为0—10之间的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 8, 2],\n",
       "       [5, 1, 1],\n",
       "       [6, 0, 3]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,10,(3,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 11.创建3x3矩阵\n",
    "备注：矩阵元素均为服从标准正态分布的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.82084149,  1.57679054, -0.28167398],\n",
       "       [ 0.78497073,  1.08230743,  0.66150165],\n",
       "       [-1.32122518, -0.50510931,  0.55786583]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn(3, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 12.将第五题的result修改为3x3矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = result.reshape(3,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 13.对上一题生成的result取转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4, 7],\n",
       "       [2, 5, 8],\n",
       "       [3, 6, 9]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 14.查看result的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.dtype\n",
    "#dtype('int64')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 15.查看result的内存占用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "72\n",
      "72\n"
     ]
    }
   ],
   "source": [
    "#方法一：直接查看\n",
    "print(result.nbytes)\n",
    "#方法2手动计算\n",
    "print(result.itemsize * 9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 16.将result的数据类型修改为float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = result.astype(float)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 17.提取result第三行第三列的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9.0"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[2,2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 18.将result第三行第三列的元素放大十倍"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "result[2,2] = result[2,2] * 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 19.提取result中的所有偶数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.,  4.,  6.,  8., 90.])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[result % 2 == 0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 20.将result中所有奇数修改为666"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "result[result % 2 == 1] = 666"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NumPy进阶修炼80题 第二期 ｜基本矩阵操作与运算\n",
    "## 公众号：早起Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 21.创建主对角线都是5的5x5矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 0, 0, 0, 0],\n",
       "       [0, 5, 0, 0, 0],\n",
       "       [0, 0, 5, 0, 0],\n",
       "       [0, 0, 0, 5, 0],\n",
       "       [0, 0, 0, 0, 5]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = np.diag([5,5,5,5,5])\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 22.交换第一列与第二列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = result[:, [1,0,2,3,4]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 23.交换第一行与第二行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = result[[0,1,2,4,3], :]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 24.判断两个矩阵是否有任何元素不同(使用22,23两题得到的矩阵)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print((a == b).all())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 25.计算两个矩阵不同元素的个数(使用22,23两题得到的矩阵)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(np.argwhere(a != b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 26.找到两个矩阵不同元素的位置(使用22,23两题得到的矩阵)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.argwhere(a != b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 27.矩阵乘法(使用22,23两题得到的矩阵)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.dot(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 28.矩阵对应元素相乘(使用22,23两题得到的矩阵)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(np.multiply(a,b))\n",
    "print('========方法2========')\n",
    "print(a * b) #方法2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 29.计算行列式(使用21题生成的矩阵)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.linalg.det(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "### 30.矩阵求逆(使用21题生成的矩阵)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.linalg.inv(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 31.将22与23题生成的np.array对象修改为np.matrix对象\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.matrix(a)\n",
    "b = np.matrix(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 32.计算上一题生成的两个np.matrix格式矩阵的对应元素乘积（对比异同）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.multiply(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "### 33.对31题生成的两个np.matrix格式矩阵做矩阵乘法（对比异同）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a * b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 34.将两个矩阵按照行拼接\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 35.将两个矩阵按照列拼接\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 36.思考下面代码运行后new的结果\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new = np.pad(result,pad_width = 1,constant_values=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 37.找到new中大于1的元素的位置\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.argwhere(new > 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 38.将new中大于1的元素修改为9\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new[new > 1] = 8\n",
    "new"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 39.对new按列求和\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(new, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 40.对new按行求和\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(new, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NumPy进阶修炼第三期｜41-60\n",
    "## 公众号：早起Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 41 生成指定格式数据\n",
    "备注：使用numpy生成6行6列的二维数组，值为1-100随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[28, 77, 35, 79, 44, 93],\n",
       "       [33, 51, 56, 56, 73, 56],\n",
       "       [53, 44, 73, 63, 29, 71],\n",
       "       [25, 90, 87, 51, 44, 30],\n",
       "       [50, 39,  7, 29, 83, 89],\n",
       "       [18,  8, 75, 33, 40, 64]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(1,100, [6,6])\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 42 找到每列的最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([53, 90, 87, 79, 83, 93])"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.amax(data, axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 43 找到每行的最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([28, 33, 29, 25,  7,  8])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.amin(data, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 44 提取data每个元素的出现次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 7,  8, 18, 25, 28, 29, 30, 33, 35, 39, 40, 44, 50, 51, 53, 56, 63,\n",
       "        64, 71, 73, 75, 77, 79, 83, 87, 89, 90, 93]),\n",
       " array([1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 3, 1, 2, 1, 3, 1, 1, 1, 2, 1, 1,\n",
       "        1, 1, 1, 1, 1, 1]))"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(data,return_counts=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 45 获取data每行元素的大小排名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 4, 1, 3, 5],\n",
       "       [0, 1, 2, 3, 5, 4],\n",
       "       [4, 1, 0, 3, 5, 2],\n",
       "       [0, 5, 4, 3, 2, 1],\n",
       "       [2, 3, 1, 0, 4, 5],\n",
       "       [1, 0, 3, 4, 5, 2]])"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.argsort()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 46 将数组按行重复一次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[28, 77, 35, 79, 44, 93],\n",
       "       [28, 77, 35, 79, 44, 93],\n",
       "       [33, 51, 56, 56, 73, 56],\n",
       "       [33, 51, 56, 56, 73, 56],\n",
       "       [53, 44, 73, 63, 29, 71],\n",
       "       [53, 44, 73, 63, 29, 71],\n",
       "       [25, 90, 87, 51, 44, 30],\n",
       "       [25, 90, 87, 51, 44, 30],\n",
       "       [50, 39,  7, 29, 83, 89],\n",
       "       [50, 39,  7, 29, 83, 89],\n",
       "       [18,  8, 75, 33, 40, 64],\n",
       "       [18,  8, 75, 33, 40, 64]])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.repeat(data, 2, axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 47 去除数组的重复行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[18,  8, 75, 33, 40, 64],\n",
       "       [25, 90, 87, 51, 44, 30],\n",
       "       [28, 77, 35, 79, 44, 93],\n",
       "       [33, 51, 56, 56, 73, 56],\n",
       "       [50, 39,  7, 29, 83, 89],\n",
       "       [53, 44, 73, 63, 29, 71]])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(data,axis = 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 48 不放回抽样\n",
    "备注：从data的第一行中不放回抽3个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([77, 35, 93])"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.choice(data[0:1][0], 3, replace=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 49 提取data第二行中不含第三行的元素的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([33, 51, 56, 56, 56])"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = data[1:2]\n",
    "b = data[2:3]\n",
    "index=np.isin(a,b)\n",
    "array=a[~index]\n",
    "array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 50 判断data是否有空行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(~data.any(axis=1)).any()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 51 将每行升序排列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[28, 35, 44, 77, 79, 93],\n",
       "       [33, 51, 56, 56, 56, 73],\n",
       "       [29, 44, 53, 63, 71, 73],\n",
       "       [25, 30, 44, 51, 87, 90],\n",
       "       [ 7, 29, 39, 50, 83, 89],\n",
       "       [ 8, 18, 33, 40, 64, 75]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.sort(axis = 1)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 52 将data的数据格式修改为float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "data1 = data.astype(float)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 53 将小于5的元素修改为nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[nan, 35., 44., 77., 79., 93.],\n",
       "       [33., 51., 56., 56., 56., 73.],\n",
       "       [29., 44., 53., 63., 71., 73.],\n",
       "       [25., 30., 44., 51., 87., 90.],\n",
       "       [ 7., 29., 39., 50., 83., 89.],\n",
       "       [ 8., 18., 33., 40., 64., 75.]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1[data1 < 5] = np.nan\n",
    "data1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 54 删除data1含有nan的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "data1 = data1[~np.isnan(data1).any(axis=1), :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[33., 51., 56., 56., 56., 73.],\n",
       "       [29., 44., 53., 63., 71., 73.],\n",
       "       [25., 30., 44., 51., 87., 90.],\n",
       "       [ 7., 29., 39., 50., 83., 89.],\n",
       "       [ 8., 18., 33., 40., 64., 75.]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 55 找出data1第一行出现频率最高的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "56.0\n"
     ]
    }
   ],
   "source": [
    "vals, counts = np.unique(data1[0,:], return_counts=True)\n",
    "print(vals[np.argmax(counts)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 56 找到data1中与100最接近的数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "90.0"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 100\n",
    "data1.flat[np.abs(data1 - a).argmin()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 57 data1每一行的元素减去每一行的平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-21.16666667,  -3.16666667,   1.83333333,   1.83333333,\n",
       "          1.83333333,  18.83333333],\n",
       "       [-26.5       , -11.5       ,  -2.5       ,   7.5       ,\n",
       "         15.5       ,  17.5       ],\n",
       "       [-29.5       , -24.5       , -10.5       ,  -3.5       ,\n",
       "         32.5       ,  35.5       ],\n",
       "       [-42.5       , -20.5       , -10.5       ,   0.5       ,\n",
       "         33.5       ,  39.5       ],\n",
       "       [-31.66666667, -21.66666667,  -6.66666667,   0.33333333,\n",
       "         24.33333333,  35.33333333]])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 - data1.mean(axis=1, keepdims=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 58 将data1归一化至区间[0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.31325301, 0.53012048, 0.59036145, 0.59036145, 0.59036145,\n",
       "        0.79518072],\n",
       "       [0.26506024, 0.44578313, 0.55421687, 0.6746988 , 0.77108434,\n",
       "        0.79518072],\n",
       "       [0.21686747, 0.27710843, 0.44578313, 0.53012048, 0.96385542,\n",
       "        1.        ],\n",
       "       [0.        , 0.26506024, 0.38554217, 0.51807229, 0.91566265,\n",
       "        0.98795181],\n",
       "       [0.01204819, 0.13253012, 0.31325301, 0.39759036, 0.68674699,\n",
       "        0.81927711]])"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.max(data1) - np.min(data1)\n",
    "(data1 - np.min(data1)) / a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 59  将data1标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.16268622,  1.41802672,  1.2856926 ,  0.52888589, -1.40282088,\n",
       "        -0.89773106],\n",
       "       [ 0.79357948,  0.82006364,  0.93504916,  1.45443618, -0.10391266,\n",
       "        -0.89773106],\n",
       "       [ 0.42447275, -0.3758625 , -0.11688115, -0.13222147,  1.28158945,\n",
       "         1.28247294],\n",
       "       [-1.23650756, -0.4612858 , -0.70128687, -0.26444294,  0.93521392,\n",
       "         1.15422565],\n",
       "       [-1.14423088, -1.40094206, -1.40257375, -1.58665766, -0.71006983,\n",
       "        -0.64123647]])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mu = np.mean(data1, axis=0)\n",
    "sigma = np.std(data1, axis=0)\n",
    "(data1 - mu) / sigma"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 60 将data1存储至本地"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('test.txt',data1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NumPy进阶修炼第四期｜NumPy最后二十问\n",
    "## 公众号：早起Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 61.如何获得两个数组之间的相同元素\n",
    "输入:\n",
    "\n",
    "arr1 = np.random.randint(10,6,6)\n",
    "\n",
    "arr2 = np.random.randint(10,6,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.random.randint(1,10,10)\n",
    "arr2 = np.random.randint(1,10,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1: [9 4 1 4 9 9 8 5 3 1]\n",
      "arr2: [1 5 8 7 9 2 6 9 1 2]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1, 5, 8, 9])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"arr1: %s\"%arr1)\n",
    "print(\"arr2: %s\"%arr2)\n",
    "np.intersect1d(arr1,arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 62.如何从一个数组中删除另一个数组存在的元素\n",
    "输入:\n",
    "\n",
    "arr1 = np.random.randint(1,10,10)\n",
    "\n",
    "arr2 = np.random.randint(1,10,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1: [3 3 2 6 6 1 4 6 2 2]\n",
      "arr2: [3 3 2 6 8 8 5 2 4 7]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.random.randint(1,10,10)\n",
    "arr2 = np.random.randint(1,10,10)\n",
    "print(\"arr1: %s\"%arr1)\n",
    "print(\"arr2: %s\"%arr2)\n",
    "np.setdiff1d(arr1,arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 63.如何修改一个数组为只读模式\n",
    "输入:\n",
    "\n",
    "arr1 = np.random.randint(1,10,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.random.randint(1,10,10)\n",
    "arr1.flags.writeable = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "assignment destination is read-only",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-31-ddcf305e5efb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m#尝试修改会报错！\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0marr1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: assignment destination is read-only"
     ]
    }
   ],
   "source": [
    "#尝试修改会报错！\n",
    "arr1[0] = 6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 64.如何将list转为numpy数组\n",
    "输入:\n",
    "\n",
    "a = [1,2,3,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "np.array(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 65.如何将pd.DataFrame转为numpy数组\n",
    "输入:\n",
    "\n",
    "df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A  B  C\n",
      "0  1  4  7\n",
      "1  2  5  8\n",
      "2  3  6  9\n",
      "[[1 4 7]\n",
      " [2 5 8]\n",
      " [3 6 9]]\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})\n",
    "print(df)\n",
    "print(df.values)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 66.如何使用numpy进行描述性统计分析\n",
    "输入：\n",
    "\n",
    "arr1 = np.random.randint(1,10,10)\n",
    "\n",
    "arr2 = np.random.randint(1,10,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1的平均数为:5.2\n",
      "arr1的中位数为:5.0\n",
      "arr1的方差为:6.56\n",
      "arr1的标准差为:2.5612496949731396\n",
      "arr1,arr的相关性矩阵为:[[7.28888889 2.6       ]\n",
      " [2.6        7.12222222]]\n",
      "arr1,arr的协方差矩阵为:[[1.         0.36085682]\n",
      " [0.36085682 1.        ]]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.random.randint(1,10,10)\n",
    "arr2 = np.random.randint(1,10,10)\n",
    "\n",
    "print(\"arr1的平均数为:%s\" %np.mean(arr1))\n",
    "print(\"arr1的中位数为:%s\" %np.median(arr1))\n",
    "print(\"arr1的方差为:%s\" %np.var(arr1))\n",
    "print(\"arr1的标准差为:%s\" %np.std(arr1))\n",
    "print(\"arr1,arr的相关性矩阵为:%s\" %np.cov(arr1,arr2))\n",
    "print(\"arr1,arr的协方差矩阵为:%s\" %np.corrcoef(arr1,arr2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 67.如何使用numpy进行概率抽样\n",
    "输入：\n",
    "\n",
    "arr = np.array([1,2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 5, 5, 5, 5, 4, 1, 5, 5, 5])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([1,2,3,4,5])\n",
    "np.random.choice(arr,10,p = [0.1,0.1,0.1,0.1,0.6])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 68.如何创建副本\n",
    "输入：\n",
    "\n",
    "arr = np.array([1,2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "#对副本数据进行修改，不会影响到原始数据\n",
    "arr = np.array([1,2,3,4,5])\n",
    "arr1 = arr.copy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 69.如何对数组切片\n",
    "输入:\n",
    "arr = np.arange(10)\n",
    "\n",
    "备注：从索引2开始到索引8停止，间隔为2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 6])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(10)\n",
    "a = slice(2,8,2)\n",
    "arr[a] #等价于arr[2:8:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 70.如何使用NumPy操作字符串\n",
    "输入:\n",
    "\n",
    "str1 = ['I love']\n",
    "\n",
    "str2 = [' Python']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['I love Python']\n",
      "['I Love Python']\n"
     ]
    }
   ],
   "source": [
    "#拼接字符串\n",
    "str1 = ['I love']\n",
    "str2 = [' Python']\n",
    "print(np.char.add(str1,str2))\n",
    "\n",
    "#大写首字母\n",
    "str3 = np.char.add(str1,str2)\n",
    "print(np.char.title(str3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 71.如何对数据向上/下取整\n",
    "输入:\n",
    "\n",
    "arr = np.random.uniform(0,10,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3.69853424 2.37607144 2.75296734 3.98645188 9.8325098  9.78061437\n",
      " 1.05052842 6.93954312 3.74869061 0.89292687]\n",
      "[ 4.  3.  3.  4. 10. 10.  2.  7.  4.  1.]\n",
      "[3. 2. 2. 3. 9. 9. 1. 6. 3. 0.]\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.uniform(0,10,10)\n",
    "print(arr)\n",
    "###向上取整\n",
    "print(np.ceil(arr))\n",
    "###向下取整\n",
    "print(np.floor(arr) )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 72.如何取消默认科学计数显示数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.set_printoptions(suppress=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 73.如何使用NumPy对二维数组逆序\n",
    "输入：\n",
    "\n",
    "arr = np.random.randint(1,10,[3,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 6 2]\n",
      " [1 6 9]\n",
      " [2 7 5]]\n",
      "列逆序\n",
      "[[2 6 5]\n",
      " [9 6 1]\n",
      " [5 7 2]]\n",
      "行逆序\n",
      "[[2 7 5]\n",
      " [1 6 9]\n",
      " [5 6 2]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.randint(1,10,[3,3])\n",
    "print(arr)\n",
    "print('列逆序')\n",
    "print(arr[:, -1::-1])\n",
    "print('行逆序')\n",
    "print(arr[-1::-1, :])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 74.如何使用NumPy根据位置查找元素\n",
    "输入：\n",
    "\n",
    "arr1 = np.random.randint(1,10,5)\n",
    "\n",
    "arr2 = np.random.randint(1,20,10)\n",
    "\n",
    "备注：在arr2中根据arr1中元素以位置查找"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 9 3 7 9]\n",
      "[ 1  3 14  5 13 15 16  6  1  6]\n",
      "[13  6  5  6  6]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.random.randint(1,10,5)\n",
    "arr2 = np.random.randint(1,20,10)\n",
    "print(arr1)\n",
    "print(arr2)\n",
    "print(np.take(arr2,arr1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 75.如何使用numpy求余数\n",
    "输入：\n",
    "\n",
    "a = 10\n",
    "\n",
    "b = 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mod(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 76.如何使用NumPy进行矩阵SVD分解\n",
    "输入：\n",
    "\n",
    "A = np.random.randint(1,10,[3,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[-0.53080534, -0.05856788, -0.84546762],\n",
       "        [-0.50084903, -0.7830816 ,  0.36869155],\n",
       "        [-0.68366361,  0.61915508,  0.38633023]]),\n",
       " array([15.54841031,  7.33950585,  1.54226802]),\n",
       " array([[-0.54854452, -0.50684335, -0.66498777],\n",
       "        [-0.83142704,  0.24649305,  0.49796612],\n",
       "        [-0.08847596,  0.82604539, -0.55661568]]))"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.svd(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 77.如何使用NumPy多条件筛选数据\n",
    "输入：\n",
    "\n",
    "arr = np.random.randint(1,20,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6 4]\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.randint(1,20,10)\n",
    "print(arr[(arr>1)&(arr<7)&(arr%2==0)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 78.如何使用NumPy对数组分类\n",
    "输入：\n",
    "\n",
    "arr = np.random.randint(1,20,10)\n",
    "\n",
    "备注：将大于等于7，或小于3的元素标记为1，其余为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 8 19 16  1  4  7 12  1 16  1]\n",
      "[ 1  1  1 -1  0  1  1 -1  1 -1]\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.randint(1,20,10)\n",
    "print(arr)\n",
    "print(np.piecewise(arr, [arr < 3, arr >= 7], [-1, 1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 79如何使用NumPy压缩矩阵\n",
    "输入：\n",
    "\n",
    "arr = np.random.randint(1,10,[3,1])\n",
    "\n",
    "备注：从数组的形状中删除单维度条目，即把shape中为1的维度去掉"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3]\n",
      " [8]\n",
      " [5]]\n",
      "[3 8 5]\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.randint(1,10,[3,1])\n",
    "print(arr)\n",
    "print(np.squeeze(arr))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 80.如何使用numpy求解线性方程组\n",
    "输入：\n",
    "\n",
    "A = np.array([[1, 2, 3], [2, -1, 1], [3, 0, -1]])\n",
    "\n",
    "b = np.array([9, 8, 3])\n",
    "\n",
    "求解Ax = b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2. -1.  3.]\n"
     ]
    }
   ],
   "source": [
    "A = np.array([[1, 2, 3], [2, -1, 1], [3, 0, -1]])\n",
    "b = np.array([9, 8, 3])\n",
    "x = np.linalg.solve(A, b)\n",
    "print(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
